package com.chatcompetition.controller;


import com.chatcompetition.common.api.CommonResult;
import com.chatcompetition.domain.entity.Employee;
import com.chatcompetition.domain.model.UmsMenu;
import com.chatcompetition.domain.vo.EmployeeLoginVO;
import com.chatcompetition.service.EmployeeService;
import com.chatcompetition.service.UmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

@RestController
@Api(tags = "EmployeeController")
@Tag(name = "EmployeeController", description = "后台用户管理")
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private UmsRoleService roleService;
    @ApiOperation(value = "登录以后返回token")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> login(@RequestBody Employee employee) {
        Integer employeeStatus = employeeService.getEmployeeStatusByEmployeeNumber(employee.getEmployeeNumber());
        if(employeeStatus == null || employeeStatus!= 1)
        {
            return ResponseEntity.status(401).body("Invalid credentials");
        }
        EmployeeLoginVO loginVO = employeeService.login(employee.getEmployeeNumber(), employee.getEmployeePassword(), employee.getEmployeeDepartment());

        if (loginVO != null) {
            return ResponseEntity.ok(loginVO);
        } else {
            return ResponseEntity.status(401).body("Invalid credentials");
        }
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @RequestMapping(value = "/menu", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getUserMenu(Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        Employee employee = employeeService.getEmployeeByEmployeeNumber(username);
        List<UmsMenu> menuList = roleService.getMenuList(employee.getId());
        return CommonResult.success(menuList);
    }



    @GetMapping("/list")
    public ResponseEntity<List<Employee>> getAllEmployees() {
        List<Employee> employees = employeeService.getAllEmployees();
        return ResponseEntity.ok(employees);
    }

    @PostMapping("/add")
    public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee) {
        employee.setEmployeeStatus(1);
        Employee newEmployee = employeeService.addEmployee(employee);
        Employee employee1 = employeeService.getEmployeeByEmployeeNumber1(employee.getEmployeeNumber());
        employeeService.addRoleRelation(employee1.getId(), employee.getEmployeeDepartment());
        return ResponseEntity.ok(newEmployee);
    }

    // 根据工号更新员工信息
    @PutMapping("/update")
    public boolean updateEmployeeByEmployeeNumber(@RequestBody Employee employee) {
        Employee employee1 = employeeService.getEmployeeByEmployeeNumber1(employee.getEmployeeNumber());
        employeeService.updateRoleRelation(employee1.getId(), employee.getEmployeeDepartment());
        return employeeService.updateEmployeeByEmployeeNumber(employee);
    }

    // 根据工号删除员工
    @DeleteMapping("/delete/{employeeNumber}")
    public boolean deleteByEmployeeNumber(@PathVariable String employeeNumber) {
        return employeeService.deleteByEmployeeNumber(employeeNumber);


    }

    // 根据工号更新员工状态
    @PutMapping("/updateStatus/{employeeNumber}/{employeeStatus}")
    public boolean updateEmployeeStatusByEmployeeNumber(
            @PathVariable String employeeNumber,
            @PathVariable int employeeStatus
    ) {
        // 直接从路径参数中获取状态值
        return employeeService.updateEmployeeStatusByEmployeeNumber(employeeNumber, employeeStatus);
    }


    // 根据工号查询员工的 employee_status
    @GetMapping("/status/{employeeNumber}")
    public ResponseEntity<Integer> getEmployeeStatusByEmployeeNumber(@PathVariable String employeeNumber) {
        Integer status = employeeService.getEmployeeStatusByEmployeeNumber(employeeNumber);
        if (status != null) {
            return ResponseEntity.ok(status);
        } else {
            return ResponseEntity.status(404).body(null);
        }
    }
}